﻿using Cinch;
using FR.SAFTAnalyserLite.Model;
using FR.SAFTAnalyserLite.ViewModels.Pane;
using MEFedMVVM.Common;
using System.Linq;

namespace FR.SAFTAnalyserLite.ViewModels
{
    public class GeneralLedgerViewModel : DocumentPartViewModel
    {
        public GeneralLedgerViewModel()
            : base("Planos contas")
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
            }
        }

        internal override void ResetSaftFile()
        {
            this.GeneralLedger = null;
        }

        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadGeneralLedger(AuditFile saft)
        {
            if (saft != null && saft.MasterFiles != null)
                this.GeneralLedger = saft.MasterFiles.GeneralLedger;
        }

        GeneralLedger[] generalLedger;
        public GeneralLedger[] GeneralLedger
        {
            get { return generalLedger; }
            set
            {
                generalLedger = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerViewModel>(x => x.GeneralLedger));
            }
        }

        string filter;
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;

                if (string.IsNullOrEmpty(filter))
                    this.GeneralLedger = SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.GeneralLedger;
                else
                {
                    this.GeneralLedger =
                        (from c in SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.GeneralLedger
                         where !string.IsNullOrEmpty(c.AccountDescription) && c.AccountDescription.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.AccountID) && c.AccountID.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.OpeningCreditBalance.ToString().IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.OpeningDebitBalance.ToString().IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0
                         select c).ToArray();
                }
            }
        }
    }
}
